<template>
  <div class="setContract">
    <a-modal title="选择审批流程" width="35%" :maskClosable="false" okText="保存" cancelText="取消" :visible="dialog.show"
      @ok="allEvents('sure')" @cancel="allEvents('cancel')">
      <div slot="footer">
        <a-button @click="allEvents('cancel')"> 取消 </a-button>
        <a-button type="primary" @click="allEvents('sure')"> 使用 </a-button>
      </div>
      <a-form-model ref="form" :rules="dialog.rules" :model="dialog.form"
        :label-col="{ span: 4, style: 'width:.7292rem;' }"
        :wrapper-col="{ span: 14, style: 'width:calc(100% - .7292rem)' }">
        <a-row :gutter="[10, 10]">
          <a-col :xl="24" :lg="24" :md="24">
            <a-form-model-item label="审批流程" prop="planNum">
              <a-select show-search optionFilterProp="children" v-model="dialog.form.planNum" placeholder="请选择审批流程"
                style="width: 100%" allowClear>
                <a-select-option v-for="item in processList" :key="item.processId" :value="item.processId">
                  {{ item.remarks }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
  import {
    processAll, //查所有流程
  } from '@/api/marketManagement/marketApprovalProcessConfiguration'

  import {
    marketInfoSaveSubmit, //保存并提交
    marketInfoSubmit
  } from '@/api/marketManagement/marketInfo/marketInfo'
  export default {
    name: 'sumbitApprove',
    components: {},
    data() {
      return {
        record: {},
        flag: false, //fasle为提交，true为保存并提交
        type: undefined,
        processList: [],
        id: undefined,
        dialog: {
          show: false,
          form: {
            planNum: undefined, //
          },
          rules: {
            planNum: [{
              required: true,
              message: '请选择审批流程',
              trigger: 'change',
            }, ],
          },
        },
      }
    },
    mounted() {},
    methods: {
      init(record, flag, type) {
        this.id = record.id || ''
        this.deptId = record.reporteCompany || ''
        this.record = record || {}
        this.flag = flag
        this.type = type
        this.getDicList()
      },
      tem() {
        if (!this.flag) {
          //提交/
          var that = this
          this.$confirm({
            title: '确定要提交审批吗?',
            okText: '确定',
            okType: 'danger',
            cancelText: '取消',
            onOk() {
              marketInfoSubmit({
                dataId: that.id,
                auditType: that.type,
                processId: that.dialog.form.planNum,
              }).then((res) => {
                that.$message.success('提交成功')
                that.allEvents('cancel')
                that.$emit('reset')
              })
            },
            onCancel() {},
          })
        } else {

          var that = this
          this.$confirm({
            title: '确定要提交审批吗?',
            okText: '确定',
            okType: 'danger',
            cancelText: '取消',
            onOk() {
              const params = Object.assign(that.record, {
                processAuditMain: {
                  auditType: that.type,
                  processId: that.dialog.form.planNum
                },
              })
              marketInfoSaveSubmit(params).then((res) => {
                that.$message.success('提交成功')
                that.allEvents('cancel')
                that.$emit('reset')
              })
            },
            onCancel() {},
          })
        }
      },
      allEvents(type, param, param1) {
        switch (type) {
          case 'sure':
            this.$refs.form.validate((valid) => {
              if (valid) {
                this.tem()
              }
            })
            break

          case 'cancel':
            this.dialog.show = false
            this.deptId = undefined
            this.dialog.form.planNum = ''
            this.$refs.form && this.$refs.form.resetFields()
            break
        }
      },
      getDicList() {
        // 审批流程
        processAll({
          processType: this.type,
          deptId: [this.deptId]
        }).then((res) => {
          this.processList = res || []
          if (this.processList.length == 0) {
            this.dialog.form.planNum = '-1'
            this.tem()
          } else if (this.processList.length == 1) {
            this.dialog.form.planNum = this.processList[0].processId
            this.tem()
          } else {
            this.dialog.show = true
            this.dialog.form.planNum = this.processList[0].processId
          }
        })
      },
    },
  }
</script>

<style lang="less" scoped>
</style>